import backtrader as bt
from file.bak.StrategyExt import StrategyExt


class DoubleSMA(StrategyExt):
    params = {
        'slowPeriod': 3,
        'fastPeriod': 20,
    }

    def __init__(self):
        StrategyExt.__init__(self)

        # Keep a reference to the "close" line in the data[0] dataseries
        self.dataclose = self.datas[0].close

        # Add a MovingAverageSimple indicator
        self.slowSma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.slowPeriod)
        self.fastSma = bt.indicators.SimpleMovingAverage(self.datas[0], period=self.params.fastPeriod)

    def next(self):
        StrategyExt.next(self)
        pre = -1
        cur = 0
        if self.fastSma[pre] > self.slowSma[pre] and self.fastSma[cur] <= self.slowSma[cur]:  # 均线下穿
            self.sell()
        elif self.fastSma[pre] < self.slowSma[pre] and self.fastSma[cur] >= self.slowSma[cur]:  # 均线上穿
            res = self.buy()
            i = 1
